﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Jinjia.Approve.IRepository.JJFlow;
using Jinjia.Approve.IService.JJFlow;
using Jinjia.Approve.IService.JJFlow.Action;
using Jinjia.Approve.Model.VM.JJFlow;

namespace Jinjia.Approve.Service.JJFlow.Action
{
    public class JJFLowActionCopy : JJFlowActionGoTo, IJJFLowActionCopy
    {
        private readonly IFlowInstanceEmpRepository _flowInstanceEmpRepository;
        private readonly IFlowNodeReferenceRepository _flowNodeReferenceRepository;

        public JJFLowActionCopy(IJJFlowInstanceService jjFlowInstanceService,
            IJJFlowTrackService jjFlowTrackService,
            IFlowInstanceEmpRepository flowInstanceEmpRepository,
            IFlowNodeReferenceRepository flowNodeReferenceRepository) : base(jjFlowInstanceService,
            jjFlowTrackService)
        {
            _flowInstanceEmpRepository = flowInstanceEmpRepository;
            _flowNodeReferenceRepository = flowNodeReferenceRepository;
        }

        public async Task<List<JjFlowInstanceVM>> GetInstanceList(string modular_key, string biz_order_id)
        {
            var jjFlowInstanceVms = await _jjFlowInstanceService.GetInstanceList(modular_key, biz_order_id);
            return jjFlowInstanceVms;
        }

        public async Task Copy(List<JjFlowInstanceVM> list, int template_id, string biz_order_id_destination)
        {
            foreach (var instance_info in list)
            {
                //需要新增申请记录
                //jj_flow_instance_emp
                var node_emp_list = await _flowInstanceEmpRepository.GetListByTemplateIdAndInstanceId(instance_info.Id, template_id);
                //jj_flow_track
                var track_list = await _jjFlowTrackService.GetListByInstanceId(instance_info.Id);
                //jj_node_reference
                var node_ref_list = await _flowNodeReferenceRepository.GetReferences(instance_info.Id);
                //jj_flow_instance
                var instance_info_copy = instance_info;
                instance_info.Id = 0;
                instance_info.BizOrderId = biz_order_id_destination;
                //保存
                await _jjFlowInstanceService.Save(instance_info_copy);


                foreach (var item in node_emp_list)
                {
                    item.id = 0;
                    item.instance_id = instance_info_copy.Id;
                }

                //保存
                await _flowInstanceEmpRepository.Save(node_emp_list);


                foreach (var item in track_list)
                {
                    item.id = 0;
                    item.instance_id = instance_info_copy.Id;
                }

                //保存
                await _jjFlowTrackService.Save(track_list);

                foreach (var item in node_ref_list)
                {
                    item.id = 0;
                    item.flow_instance_id = instance_info_copy.Id;
                }

                await _flowNodeReferenceRepository.Save(node_ref_list);
            }
        }
    }
}